layui.use(['layer', 'form', 'table', 'admin', 'ax', 'func', 'laydate', 'upload'], function () {
    var $ = layui.$;
    var layer = layui.layer;
    var form = layui.form;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var func = layui.func;
    var laydate = layui.laydate;
    var upload = layui.upload;
    //渲染时间选择框
    laydate.render({
        elem: '#loanDate',
        value: new Date()
    });
    /**
     * 系统管理--消息管理
     */
    var Notice = {
        tableId: "noticeTable"    //表格id
    };
    $("#condition").on("input", function (e) {
        //获取input输入的值
        Notice.search();
    });
    form.on('select(dm)', function (data) {
        Notice.search();
    });
    /**
     * 选中行效果
     */
    table.on('row(noticeTable)', function (obj) {
        $(obj.tr).siblings().css("background-color", "");
        obj.tr.css({"background-color": "#dadad9"});
    });

    /**
     * 初始化表格的列
     */
    Notice.initColumn = function () {
        return [[
            {type: 'checkbox', LAY_CHECKED: true, hide: true},
            {field: 'id', align: "center", hide: true, sort: true, title: 'id'},
            //{field: 'borrower_id', align: "center",sort: true, title: 'borrower_id'},
            {align: 'center', type: "numbers", title: '序号'},
            {align: 'center', field: "dealTime", title: '交易时间'},
            {field: 'fkmc', align: "center", sort: true, title: '付款账户名称',width:150},
            {field: 'fkzh', align: "center", sort: true, title: '付款账户',width:150},
            {field: 'skrzh', align: "center", sort: true, title: '收款人帐号',width:150},
            {field: 'name', align: "center", sort: true, title: '收款人姓名',width:150},
            {field: 'skzhBank', align: "center", sort: true, title: '收款账户所在分行',width:200},
            {field: 'zzje', align: "center", sort: true, title: '转账金额', totalRow: true,width:150},
            {field: 'remark', align: "center", sort: true, title: '附言', totalRow: true},
            {field: 'status', align: "center", sort: true, title: '状态'},
            {field: 'sxf', align: "center", sort: true, title: '手续费'},
            {field: 'ways', align: "center", sort: true, title: '转账方式',width:150},
            {field: 'fkzhMoney', align: "center", sort: true, title: '付款账户余额',width:150},
            //{field: 'dealTime', align: "center", sort: true, title: '交易时间',width:150},
            {field: 'voucher', align: "center", sort: true, title: '凭证号'},
            {field: 'daily', align: "center", sort: true, title: '本日备注',width:150},
            {field: 'monthily', align: "center", sort: true, title: '本月备注',width:150},
            {field: 'sbyy', align: "center", sort: true, title: '失败原因',width:150},
            {
                field: 'flag', align: "center", sort: true, title: '打印状态',width:150, templet: function (d) {
                    let a = "";
                    if (d.flag == "1") {
                        a = "未打印";
                    } else if (d.flag == "2") {
                        a = "已打印";
                    }
                    return a;
                }
            },
            {align: 'center', toolbar: '#tableBar', title: '操作', minWidth: 200}
        ]];
    };

    /**
     * 点击查询按钮
     */
    Notice.search = function () {
        var queryData = {};
        queryData['condition'] = $("#name").val();
        queryData['flag'] = $("#flag").val();
        table.reload(Notice.tableId, {
            where: queryData, page: {curr: 1}
        });
    };


    Notice.txt = function () {
        func.open({
            height: 200,
            title: '打款明细管理导入Txt',
            content: Feng.ctxPath + '/btyDkmx/upload',
            tableId: Notice.tableId
        });
    };
    /**
     * 点击删除通知
     *
     * @param data 点击按钮时候的行数据
     */
    Notice.onDeleteNotice = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/btyDkmx/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(Notice.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };
    /**
     * 点击修改档案
     *
     * @param data 点击按钮时候的行数据
     */
    Notice.onEditNotice = function (data) {
        func.open({
            height: 1000,
            title: '修改巡店',
            content: Feng.ctxPath + "/btyXdgt/btyXdgt_update/" + data.id,
            tableId: Notice.tableId
        });
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + Notice.tableId,
        url: Feng.ctxPath + '/btyDkmx/list',
        page: true,
        height: "full-118",
        cellMinWidth: 100,
        totalRow: true,
        cols: Notice.initColumn(),
        done: function () {
            var index = -1;
            $("[data-field='flag']").children().each(function () {
                index++;
                if ($(this).text() === '未打印') {
                    $('tr').eq(index).css("background", '#e1e2e6');
                }
            });
        },
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        Notice.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        Notice.openAddNotice();
    });
    // 导入Txt按钮点击事件
    $('#btnTxt').click(function () {
        Notice.txt();
    });
    // 打印按钮点击事件
    $('#btnPrint').click(function () {
        batchPrint();
    });

    // 工具条点击事件
    table.on('tool(' + Notice.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            Notice.onEditNotice(data);
        } else if (layEvent === 'delete') {
            Notice.onDeleteNotice(data);
        } else if (layEvent === 'print') {
            print(data, "0");
        }
    });

    //打印事件
    function print(result, a) {
        let id = "";
        if (a == 1) {
            for (let i = 0; i < result.length; i++) {
                $("#print").append('<table class="layui-table" id="test" lay-filter="noticeTable" style="width: 98%;margin: 0 auto">\n' +
                    '<colgroup>\n' +
                    '<col width="150">\n' +
                    '<col width="150">\n' +
                    '<col width="200">\n' +
                    '<col>\n' +
                    '</colgroup>\n' +
                    '<thead>\n' +
                    '<tr>\n' +
                    '<th>付款账户名称</th><th>' + result[i].fkmc + '</th>\n' +
                    '<th>收款人姓名</th><th>' + result[i].name + '</th>\n' +
                    '</tr>\n' +
                    '</thead>\n' +
                    '<tbody>\n' +
                    '<tr><td>付款账户</td><td>' + result[i].fkzh + '</td>\n' +
                    '<td>收款人账号</td><td>' + result[i].skrzh + '</td></tr>\n' +
                    '<tr><td>收款账户所在分行</td><td>' + result[i].skzhBank + '</td>\n' +
                    '<td>币种</td><td>人民币</td></tr>\n' +
                    '<tr><td>转账金额</td><td>' + result[i].zzje + '</td>\n' +
                    '<td>手续费</td><td>' + result[i].sxf + '</td></tr>\n' +
                    '<tr><td>附言</td><td colspan="3">' + result[i].remark + '</td></tr>\n' +
                    '<tr><td>转账方式</td><td colspan="3">' + result[i].ways + '</td></tr>\n' +
                    '<tr><td>付款账户余额</td><td>' + result[i].fkzhMoney + '</td>\n' +
                    '<td>交易时间</td><td>' + result[i].dealTime + '</td></tr>\n' +
                    '<tr><td>凭证号</td><td colspan="3">' + result[i].voucher + '</td></tr>\n' +
                    '<tr><td colspan="4">' + result[i].daily + '</td></tr>\n' +
                    '<tr><td colspan="4">' + result[i].monthily + '</td></tr>\n' +
                    '</tbody>\n' +
                    '</table>');
                id += result[i].id + ",";
            }
        } else {
            $("#print").html('<table class="layui-table" id="test" lay-filter="noticeTable" style="width: 98%;margin: 0 auto">\n' +
                '<colgroup>\n' +
                '<col width="150">\n' +
                '<col width="150">\n' +
                '<col width="200">\n' +
                '<col>\n' +
                '</colgroup>\n' +
                '<thead>\n' +
                '<tr>\n' +
                '<th>付款账户名称</th><th>' + result.fkmc + '</th>\n' +
                '<th>收款人姓名</th><th>' + result.name + '</th>\n' +
                '</tr>\n' +
                '</thead>\n' +
                '<tbody>\n' +
                '<tr><td>付款账户</td><td>' + result.fkzh + '</td>\n' +
                '<td>收款人账号</td><td>' + result.skrzh + '</td></tr>\n' +
                '<tr><td>收款账户所在分行</td><td>' + result.skzhBank + '</td>\n' +
                '<td>币种</td><td>人民币</td></tr>\n' +
                '<tr><td>转账金额</td><td>' + result.zzje + '</td>\n' +
                '<td>手续费</td><td>' + result.sxf + '</td></tr>\n' +
                '<tr><td>附言</td><td colspan="3">' + result.remark + '</td></tr>\n' +
                '<tr><td>转账方式</td><td colspan="3">' + result.ways + '</td></tr>\n' +
                '<tr><td>付款账户余额</td><td>' + result.fkzhMoney + '</td>\n' +
                '<td>交易时间</td><td>' + result.dealTime + '</td></tr>\n' +
                '<tr><td>凭证号</td><td colspan="3">' + result.voucher + '</td></tr>\n' +
                '<tr><td colspan="4">' + result.daily + '</td></tr>\n' +
                '<tr><td colspan="4">' + result.monthily + '</td></tr>\n' +
                '</tbody>\n' +
                '</table>');
            id = result.id;
        }
        let newstr = $("#print").html();
        $("body").html(newstr);
        updateFlag(id);
        window.print();
        window.location.reload();
    }

    //批量打印点击触发事件
    function batchPrint() {
        //获取导出的数据
        var ajax = new $ax("/btyDkmx/msg");
        var flag = $("#flag").val();
        ajax.set("flag", flag);
        var result = ajax.start();
        var checkRows = table.checkStatus(Notice.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要打印的数据");
        } else {
            print(result, 1);
        }
    };

    //修改打印状态
    function updateFlag(id) {
        //获取导出的数据
        var ajax = new $ax("/btyDkmx/updateFlag");
        ajax.set("id", id);
        var result = ajax.start();
        if (result == 0) {
            Feng.error("修改失败，请联系管理员");
        } else {
            Feng.success("已完成");
        }
    };
});